package org.ikastola

import ikastola.Constants;
import org.springframework.dao.DataIntegrityViolationException
import grails.plugins.springsecurity.Secured


@Secured(['ROLE_ADMIN'])
class PaymentCheckController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index() {
        redirect(action: "list", params: params)
    }

    def list(Integer max) {
        params.max = Math.min(max ?: 10, 100)
        [paymentCheckInstanceList: PaymentCheck.findAllByDeleted(Constants.NOT_DELETED,params), paymentCheckInstanceTotal: PaymentCheck.countByDeleted(Constants.NOT_DELETED)]
    }

    /*def create() {
        [paymentCheckInstance: new PaymentCheck(params)]
    }

    def save() {
        def paymentCheckInstance = new PaymentCheck(params)
        if (!paymentCheckInstance.save(flush: true)) {
            render(view: "create", model: [paymentCheckInstance: paymentCheckInstance])
            return
        }

        flash.message = message(code: 'default.created.message', args: [message(code: 'paymentCheck.label', default: 'PaymentCheck'), paymentCheckInstance.id])
        redirect(action: "show", id: paymentCheckInstance.id)
    }*/

    def show(Long id) {
        def paymentCheckInstance = PaymentCheck.get(id)
        if (!paymentCheckInstance || Constants.DELETED.equals(paymentCheckInstance.getDeleted())) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'paymentCheck.label', default: 'PaymentCheck'), id])
            redirect(action: "list")
            return
        }

        [paymentCheckInstance: paymentCheckInstance]
    }

    /*def edit(Long id) {
        def paymentCheckInstance = PaymentCheck.get(id)
        if (!paymentCheckInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'paymentCheck.label', default: 'PaymentCheck'), id])
            redirect(action: "list")
            return
        }

        [paymentCheckInstance: paymentCheckInstance]
    }

    def update(Long id, Long version) {
        def paymentCheckInstance = PaymentCheck.get(id)
        if (!paymentCheckInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'paymentCheck.label', default: 'PaymentCheck'), id])
            redirect(action: "list")
            return
        }

        if (version != null) {
            if (paymentCheckInstance.version > version) {
                paymentCheckInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                          [message(code: 'paymentCheck.label', default: 'PaymentCheck')] as Object[],
                          "Another user has updated this PaymentCheck while you were editing")
                render(view: "edit", model: [paymentCheckInstance: paymentCheckInstance])
                return
            }
        }

        paymentCheckInstance.properties = params

        if (!paymentCheckInstance.save(flush: true)) {
            render(view: "edit", model: [paymentCheckInstance: paymentCheckInstance])
            return
        }

        flash.message = message(code: 'default.updated.message', args: [message(code: 'paymentCheck.label', default: 'PaymentCheck'), paymentCheckInstance.id])
        redirect(action: "show", id: paymentCheckInstance.id)
    }*/

	/**
	 * La baja se hace logica y desde el receipt
	 */
    /*def delete(Long id) {
        def paymentCheckInstance = PaymentCheck.get(id)
        if (!paymentCheckInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'paymentCheck.label', default: 'PaymentCheck'), id])
            redirect(action: "list")
            return
        }

        try {
            paymentCheckInstance.delete(flush: true)
            flash.message = message(code: 'default.deleted.message', args: [message(code: 'paymentCheck.label', default: 'PaymentCheck'), id])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e) {
            flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'paymentCheck.label', default: 'PaymentCheck'), id])
            redirect(action: "show", id: id)
        }
    }*/
}
